﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Jy123.BLL.Public;
using Jy123.Model;

namespace Jy123.BLL.User
{
    public class Album
    {
        
        /// <summary>
        /// 我的相册列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet AlbumList_My(int cpage, int psize, string strWhere,string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = strSort,
                Tables = "V_User_AlbumList"
            }, out counts);
        }
        /// <summary>
        /// 相册随便看看
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strSort">排序字段</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet AlbumList_Random(int cpage, int psize, string strWhere, string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = strSort,
                Tables = "V_User_AlbumList_Random"
            }, out counts);
        }
        /// <summary>
        /// 我的相册列表(Mini)
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet AlbumList_MiniContent(int cpage, int psize, string strWhere,string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = strSort,
                Tables = "V_User_MiniContentAlbum"
            }, out counts);
        }

        
        /// <summary>
        /// 好友的相册列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="userid">用户ID</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet AlbumList_MyFriend(int cpage, int psize, int userid, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = "(FUser IN (SELECT FFriend FROM User_Friend WHERE (FUser = '" + userid + "')))AND (Privacy <> 3) AND (FUser <> '" + userid + "')",
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_User_AlbumList"
            }, out counts);
        }


        /// <summary>
        /// 后台照片列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public DataSet ManagePhotoList(int cpage, int psize, string strWhere,out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_User_PhotoList"
            }, out counts);
        }
        /// <summary>
        /// 照片评论列表
        /// </summary>
        /// <param name="cpage">当前页</param>
        /// <param name="psize">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public DataSet PhotoCommentList(int cpage, int psize, string strWhere)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_User_PhotoCommentList"
            });
        }
        /// <summary>
        /// 后台相册列表
        /// </summary>
        /// <param name="count">需要获取的数量</param>
        /// <param name="strWhere">筛选条件</param>
        /// <returns>数据集</returns>
        public DataSet ManageAlbumList(int cpage, int psize, string strWhere, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Tables = "V_Manage_User_AlbumList"
            }, out counts);
 
        }

        /// <summary>
        /// 设置相册封面
        /// </summary>
        /// <param name="photoID">照片的ID</param>
        /// <returns>是否设置成功</returns>
        public bool AlbumSetCover(int photoID)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            strSql.Append("update [User_Photo] set Iscover=(1) where (ID=@PhotoID) ");

            strSql2.Append("update [User_Photo] set Iscover=(0) where (ID<>@PhotoID) and FAlbum=(select FAlbum from [User_Photo] where (ID=@PhotoID)) ");

            SqlParameter[] parameters = {
                                        new SqlParameter("@PhotoID",SqlDbType.Int,4)};
            parameters[0].Value = photoID;

            if (DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0 & DBUtility.DbHelperSQL.ExecuteSql(strSql2.ToString(), parameters) > 0)
            {
                return true;
            }
            return false;

        }


        /// <summary>
        /// 删除图片记录集
        /// </summary>
        /// <param name="ids">图片的ID集合以逗号隔开</param>
        /// <returns>受影响行数</returns>
        public int DelPhoto(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            string sql = "delete User_Photo where id in ({0})";
            
            StringBuilder strid = new StringBuilder();
            string[] idss = ids.Split(',');
            SqlParameter[] parameters = new SqlParameter[idss.Length];
            int i = 0;
            foreach (string id in idss)
            {
                strid.Append(string.Format("@id{0}", i));
                strid.Append(",");
                parameters[i] = new SqlParameter(string.Format("@id{0}", id), SqlDbType.Int, 4);
                parameters[i].Value = id;
                i++;
            }
            strSql.AppendFormat(sql, strid.ToString().Substring(0, strid.ToString().Length - 1));
            return DBUtility.DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 批量更新照片信息
        /// </summary>
        /// <param name="hidPhotoIDs">照片的ID集合(以逗号分开)</param>
        /// <param name="txtDescriptions">照片描述</param>
        /// <returns>更新数量</returns>
        public int UpdatePhotos(string hidPhotoIDs,string txtDescriptions)
        {
            string[] strsDescript = txtDescriptions.Split(',');
            string descript = "";
            string[] hidPhotoIDss = hidPhotoIDs.Split(',');
            string hidPhotoID = "";
            StringBuilder sbSql = new StringBuilder();
            for (int i = 0; i < hidPhotoIDss.Length; i++)
            {
                hidPhotoID = hidPhotoIDss.GetValue(i).ToString();
                descript = strsDescript.GetValue(i).ToString();
                string strSql = "update User_Photo set Description= '" + descript + "' where ID = '" + hidPhotoID + "' ";
                sbSql.Append(strSql);
            }
            return DBUtility.DbHelperSQL.ExecuteSql(sbSql.ToString());
        }
        /// <summary>
        /// 统计用户拥有的相册数量
        /// </summary>
        /// <param name="userID">被统计的用户的ID</param>
        /// <returns>相册数量</returns>
        public int AlbumCount(string userIDs)
        {            
            StringBuilder sbSql = new StringBuilder();
            string sql = "select count(*)as num from user_Album where Fuser IN ({0})";

            StringBuilder sbid = new StringBuilder();
            string[] idss = userIDs.Split(',');

            SqlParameter[] parameters = new SqlParameter[idss.Length];

            int i = 0;
            foreach (string id in idss)
            {
                sbid.Append(string.Format("@id{0}", id));
                sbid.Append(",");
                parameters[i] = new SqlParameter(string.Format("@id{0}", id), SqlDbType.Int, 4);
                parameters[i].Value = id;
                i++;
            }
            sbSql.AppendFormat(sql,sbid.ToString().Substring(0,sbid.ToString().Length - 1));
            return (int)DBUtility.DbHelperSQL.GetSingle(sbSql.ToString(),parameters);
        }
        /// <summary>
        /// 根据相册ID统计相片数量
        /// </summary>
        /// <param name="albumID">相册ID</param>
        /// <returns>相片数量</returns>
        public int PhotoCount(int albumID)
        {
            string sql = "select count(*)as num from User_Photo where falbum =@AlbumID";
            SqlParameter[] parameters = { 
                                        new SqlParameter("@AlbumID", SqlDbType.Int, 4)};
            parameters[0].Value = albumID;
            return (int)DBUtility.DbHelperSQL.GetSingle(sql,parameters);
        }

        /// <summary>
        /// 根据用户ID统计用户照片数量
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>照片数量</returns>
        public int PhotoCountByUserID(int userID)
        {
            string sql = "select count(*)as num from User_Photo where fuser=@UserID";
            SqlParameter[] parameters = { 
                                        new SqlParameter("@UserID", SqlDbType.Int, 4)};
            parameters[0].Value = userID;
            return (int)DBUtility.DbHelperSQL.GetSingle(sql, parameters);
        }
        /// <summary>
        /// 统计对应相片的评论数量
        /// </summary>
        /// <param name="photoID">相片的ID</param>
        /// <returns>评论数量</returns>
        public int PhotoCommentCount(int photoID)
        {
            string sql = "select count(*)as num from Photo_Comment where FPhoto =@PhotoID";
            SqlParameter[] parameters = { 
                                        new SqlParameter("@PhotoID", SqlDbType.Int, 4)};
            parameters[0].Value = photoID;
            return (int)DBUtility.DbHelperSQL.GetSingle(sql,parameters);
        }

        /// <summary>
        /// 根据相册查找所属照片列表
        /// </summary>
        /// <param name="albumID">相册的ID</param>
        /// <param name="sort">排序方式</param>
        /// <param name="cPage">起始页</param>
        /// <param name="pageSize">每页记录数</param>
        /// <returns>数据集</returns>
        public DataSet PhotoList(int albumID,string sort,int cPage,int pageSize)
        {
            Jy123.Model.MPagination modelPag = new MPagination();
            StringBuilder sbFilter = new StringBuilder();
            sbFilter.AppendFormat("FAlbum = {0}",albumID);
            modelPag.Tables = "V_User_PhotoList";
            modelPag.PrimaryKey = "ID";
            modelPag.Fields = "*";
            modelPag.Filter = sbFilter.ToString();
            modelPag.Sort = sort;
            modelPag.CurrentPage = cPage;
            modelPag.PageSize = pageSize;
            return Pagination.GetList(modelPag);
        }

        /// <summary>
        /// 根据相册ID查找相册封面
        /// </summary>
        /// <param name="albumID">相册ID</param>
        /// <returns>数据集</returns>
        public string AlbumCover(int albumID)
        {
            string strSql = "SELECT ID, FAlbum, FUser, IsCover, FilePath FROM User_Photo WHERE (FAlbum = @albumID) AND (IsCover = 1)";
            SqlParameter[] parameters = {
                                        new SqlParameter("@albumID",SqlDbType.Int,4)};
            parameters[0].Value = albumID;
            string cover =string.Empty;
            try
            {
               cover = DBUtility.DbHelperSQL.Query(strSql, parameters).Tables[0].Rows[0]["Filepath"].ToString();
            }
            catch { cover = null; }
            
            if (string.IsNullOrEmpty(cover))
            {
                return Jy123.Common.Config.BaseConfig.GetConfigValue("AlbumCover");
            }

            return DBUtility.DbHelperSQL.Query(strSql, parameters).Tables[0].Rows[0]["Filepath"].ToString();
        }
        /// <summary>
        /// 照片分页
        /// </summary>
        /// <param name="cpage">起始页</param>
        /// <param name="psize">每页记录数</param>
        /// <param name="strWhere">筛选条件</param>
        /// <param name="strSort">排序方式</param>
        /// <param name="counts">总数[0],页数[1]</param>
        /// <returns>数据集</returns>
        public DataSet Photo(int cpage, int psize, string strWhere, string strSort, out List<int?> counts)
        {
            return Pagination.GetList(new MPagination()
            {
                Filter = (string.IsNullOrEmpty(strWhere)) ? null : strWhere,
                CurrentPage = cpage,
                Fields = "*",
                PageSize = psize,
                PrimaryKey = "ID",
                Sort = strSort,
                Tables = "User_Photo"
            }, out counts);
        }
        /// <summary>
        /// 根据照片ID删除照片评论
        /// </summary>
        /// <param name="photoID"></param>
        public int DelPhotoComment_ByPhotoID(int photoID)
        {
            string strSql = "DELETE FROM Photo_Comment WHERE (FPhoto = @PhotoID)";
            SqlParameter[] parameters = {
                                        new SqlParameter("@PhotoID",SqlDbType.Int,4)};
            parameters[0].Value = photoID;
            return DBUtility.DbHelperSQL.ExecuteSql(strSql, parameters);
        }
        /// <summary>
        /// 查看照片
        /// </summary>
        /// <param name="albumID">相册ID</param>
        /// <param name="photoID">照片ID</param>
        /// <returns>数据集</returns>
        public DataSet ViewPhoto(int albumID, int photoID,out int lastID,out int nextID)
        {
            SqlParameter[] parameters = {
                                        new SqlParameter("@Album",SqlDbType.Int,4),
                                        new SqlParameter("@ID",SqlDbType.Int,4),
                                        new SqlParameter("@LastID",SqlDbType.Int),
                                        new SqlParameter("@NextID",SqlDbType.Int)};
            parameters[0].Value = albumID;
            parameters[1].Value = photoID;
            parameters[2].Direction = ParameterDirection.Output;
            parameters[3].Direction = ParameterDirection.Output;
            DataSet ds = DBUtility.DbHelperSQL.RunProcedure("SP_User_Photo", parameters, "ds");
            lastID = (parameters[2].Value != DBNull.Value) ? Convert.ToInt32(parameters[2].Value) : 0;
            nextID = (parameters[3].Value != DBNull.Value) ? Convert.ToInt32(parameters[3].Value) : 0;
            return ds;
        }

        /// <summary>
        /// 取得用户头像的图片ID
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>图片的ID</returns>
        public int GetPortraitID(int userID)
        {
            int photoID = 0;
            string strSql = "select id from dbo.User_Photo where fuser=@UserID and phototype=0";
            SqlParameter[] parameter = { 
                                       new SqlParameter("@UserID",SqlDbType.Int)};
            parameter[0].Value = userID;
            photoID=(int)DBUtility.DbHelperSQL.GetSingle(strSql,parameter);
            return photoID;
        }


    }
}
